package com.cy.shiro.controller;

import com.cy.shiro.pojo.SysResources;
import com.cy.shiro.pojo.SysRole;
import com.cy.shiro.pojo.SysUserRole;
import com.cy.shiro.service.SysRoleResourcesService;
import com.cy.shiro.service.SysRoleService;
import com.cy.shiro.service.SysUserRoleService;
import com.cy.shiro.util.ResultUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SysRoleController {

    @Autowired
    private BaseController baseController;
    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleResourcesService sysRoleResourcesService;

    //角色管理
    @RequestMapping("/roles")
    public String roles(Model model){
        //资源权限
        baseController.sysResources("角色管理","/roles", model);

        return "/role/roles";
    }

    //权限列表post请求
    @GetMapping("/roles/list")
    @ResponseBody
    @CrossOrigin
    public Object list(HttpServletRequest request){
        String limit = request.getParameter("limit") == null ? "10" : request.getParameter("limit");
        String offset = request.getParameter("offset");
        String page = request.getParameter("page");
        String sort = "desc";

        if(limit == null){
            limit = "10";
        }
        Map<String, String> map = new HashMap<String, String>();
        map.put("limit", limit);
        map.put("offset", offset);
        map.put("page", page);
        map.put("sort", sort);
        map.put("name", request.getParameter("name"));

        PageHelper.startPage(Integer.valueOf(page),Integer.valueOf(limit));

        List<SysRole> list = sysRoleService.list();

        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        System.out.println("pageInfo:"+pageInfo.getTotal());

        int total = Integer.valueOf((int) pageInfo.getTotal());

        return  ResultUtil.bootstrapTableJson(pageInfo.getList(), total, map);
    }


    //权限列表post请求
    @PostMapping("/roles/useList")
    @ResponseBody
    @CrossOrigin
    public Object list(){

        List<SysRole> list = sysRoleService.useList();
        System.out.println("权限列表："+ list);

        return ResultUtil.success("success", list);
    }

    //添加角色
    @PostMapping("/roles/addPost")
    @ResponseBody
    @CrossOrigin
    public Object addPost(SysRole sysRole){

        Integer id = sysRoleService.addPost(sysRole);

        if("0".equals(String.valueOf(id)) || "null".equals(String.valueOf(id))){
            return ResultUtil.error("添加失败");
        }

        return ResultUtil.success("添加成功");
    }

    //编辑保存
    @PostMapping("/roles/editPost")
    @ResponseBody
    @CrossOrigin
    public Object editPost(SysRole sysRole){

        Integer integer = sysRoleService.editPost(sysRole);
        if("0".equals(String.valueOf(integer)) || "null".equals(String.valueOf(integer))){
            return ResultUtil.error("保存失败");
        }

        return ResultUtil.success("保存成功");
    }

    //删除
    @PostMapping("/roles/delPost")
    @ResponseBody
    @CrossOrigin
    public Object delPost(Integer id){

        Integer integer = sysRoleService.delPost(id);
        if("0".equals(String.valueOf(integer)) || "null".equals(String.valueOf(integer))){
            return ResultUtil.error("删除失败");
        }

        return ResultUtil.success("删除成功");
    }

    //保存修改资源权限
    @PostMapping("/roles/saveRoleResources")
    @ResponseBody
    @CrossOrigin
    public Object saveRoleResources(Integer roleId, String resourcesIds){

        System.out.println("roleId:"+ roleId);
        System.out.println("resourcesIds:"+ resourcesIds);

        Integer integer = sysRoleResourcesService.addRoleResources(roleId, resourcesIds);
        System.out.println("修改权限：" + integer);

        return ResultUtil.success("修改成功");
    }

}
